//
//  PopBottomView.m
//  lyx
//
//  Created by apple on 2017/10/25.
//  Copyright © 2017年 seeday. All rights reserved.
//

#import "ViesionDataView.h"
#import "ViesionDataCell.h"
#import "Sizes.h"
#import "UIButton+Gradient.h"
#import "UIView+ZFFrame.h"
#import "Colors.h"

@implementation ViesionDataView

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.bounds = [UIScreen mainScreen].bounds;
        self.backgroundColor =[[UIColor blackColor] colorWithAlphaComponent:0.3];

        self.view_bg = [[UIView alloc]initWithFrame:CGRectMake(36, ScreenHeight/2-150, ScreenWidth-72,300)];
        self.view_bg.backgroundColor = [UIColor whiteColor];
        self.view_bg.layer.cornerRadius = 10;
        self.view_bg.layer.masksToBounds = YES;
        self.imageView = [[UIImageView alloc] init];
        self.imageView.image = [UIImage imageNamed:@"ic_version_bg"];
        
        self.cancelBtn = [TYMButton btn_Image:@"icon_equipment12"];
        [self.cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
        UIView *bgimage = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth-72, 123.5)];
        bgimage.backgroundColor = rgba(237, 246, 255);

        self.versionLab = [[UILabel alloc] initWithFrame:CGRectMake(22, bgimage.zf_bottom + 15 , 200, 18)];
        self.versionLab.layer.masksToBounds = YES;
        self.versionLab.layer.cornerRadius = 8;
        self.versionLab.backgroundColor = [UIColor whiteColor];
        self.versionLab.font = Font_Medium(18);
        self.versionLab.text = @"发现新版本";
        [self.versionLab setTextAlignment:NSTextAlignmentLeft];
        [self.versionLab setTextColor:rgba(74, 74, 74)];
        [self.view_bg addSubview:self.versionLab];
        self.versionStrLab = [[WKWebView alloc] init];
       
        self.versionStrLab.frame = CGRectMake(15, 178, self.view_bg.zf_width-30, 80);
        [self.view_bg addSubview:self.versionStrLab];
    
        self.updatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.updatBtn.frame = CGRectMake(25, self.view_bg.zf_height-59, self.view_bg.zf_width-50, 44);
         [self.updatBtn gradientButtonWithSize:CGSizeMake(300, 44) colorArray:@[(id)rgba(139, 175, 255),(id)rgba(76, 132, 255)] percentageArray:@[@(0.5),@(1)] gradientType:GradientFromLeftTopToRightBottom];
        self.updatBtn.layer.cornerRadius = 5;
        self.updatBtn.layer.masksToBounds = YES;
        [self.updatBtn setTitle:@"现在升级" forState:UIControlStateNormal];
        
        [self.updatBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.updatBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
        [self.updatBtn addTarget:self action:@selector(updateAppAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view_bg addSubview:self.updatBtn];
    }
    return self;
}

-(void)updateAppAction:(id)sender{
    [self removeOperateView];
    
    if (self.blockCallBackIndex) {
        self.blockCallBackIndex();
    }
}

-(void)setData:(VersionModel *)data{
    _data = data;
    [self.versionStrLab loadHTMLString:self.data.versionDesc baseURL:nil];
    self.versionLab.text = [NSString stringWithFormat:@"发现新版本:%@",data.versionNo];
}

-(void)cancelAction:(id)sender{
    
    [self removeOperateView];
}

-(void)viewShow{
    
    CGFloat atherH = 178 + 74;
    CGFloat tableViewH = 70;
    self.view_bg.frame = CGRectMake(36, ScreenHeight/2-(atherH+tableViewH)/2, ScreenWidth-72, atherH+tableViewH);
    self.imageView.frame = CGRectMake(ScreenWidth/2-169/4, self.view_bg.zf_top-20, 169, 143);
    self.cancelBtn.frame = CGRectMake(ScreenWidth/2-16, self.view_bg.zf_bottom + 30, 32, 32);
    self.updatBtn.frame = CGRectMake(25, self.view_bg.zf_height-44-15, self.view_bg.zf_width-50, 44);
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow addSubview:self.view_bg];
    [[UIApplication sharedApplication].keyWindow addSubview:self.imageView];
    [[UIApplication sharedApplication].keyWindow addSubview:self.cancelBtn];
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0.5;
        self.view_bg.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}

-(void)removeOperateView{
    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0.0;
        self.view_bg.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.view_bg removeFromSuperview];
        [self.cancelBtn removeFromSuperview];
        [self.imageView removeFromSuperview];
    }];
}


@end
